import { defineStore } from 'pinia'
import { reactive, ref } from 'vue'

export const useStore = defineStore(
  'main',
  () => {
    const someState = ref('hello pinia')
    const userinfo = reactive({
        usertoken:"",
        userid:"",
        username:"",
        realname:"",
        captchaid:"",
        role: 0, // 用户角色ID：1,2为管理员
        permissions: [] as string[] // 用户具体权限列表，默认包含增删改查
    })
    
    // 初始化默认权限
    const initDefaultPermissions = () => {
      // 清空现有权限，避免重复
      userinfo.permissions = []
      
      // 如果是管理员角色(ID为1或2)，添加所有默认权限
      if (userinfo.role === 1 || userinfo.role === 2) {
        // 所有模块的完整权限
        const defaultPermissions = [
          // 用户模块权限
          'user:add', 'user:edit', 'user:delete', 'user:view',
          
          // 角色模块权限
          'role:add', 'role:edit', 'role:delete', 'role:view',
          
          // 部门模块权限
          'department:add', 'department:edit', 'department:delete', 'department:view',
          
          // 会议室模块权限
          'room:add', 'room:edit', 'room:delete', 'room:view',
          
          // 会议预约模块权限
          'meeting:add', 'meeting:edit', 'meeting:delete', 'meeting:view',
          
          // 会议审批模块权限
          'meeting:approve', 'meeting:view'
        ]
        
        // 添加默认权限
        userinfo.permissions = [...defaultPermissions]
      } else if (userinfo.role === 3) {
        // 普通用户权限
        const userPermissions = [
          // 会议室查看权限
          'room:view',
          
          // 会议预约权限
          'meeting:add', 'meeting:edit', 'meeting:view'
        ]
        
        // 添加普通用户权限
        userinfo.permissions = [...userPermissions]
      } else if (userinfo.role === 1007) {
        // 部门经理权限
        const deptManagerPermissions = [
          // 部门管理权限
          'department:view', 'department:edit',
          
          // 会议室查看权限
          'room:view',
          
          // 会议预约与审批权限
          'meeting:add', 'meeting:edit', 'meeting:view', 'meeting:approve'
        ]
        
        userinfo.permissions = [...deptManagerPermissions]
      } else if (userinfo.role === 2011) {
        // 产品经理权限
        const productManagerPermissions = [
          // 会议室管理权限
          'room:add', 'room:edit', 'room:delete', 'room:view',
          
          // 会议预约权限
          'meeting:add', 'meeting:edit', 'meeting:view'
        ]
        
        userinfo.permissions = [...productManagerPermissions]
      } else if (userinfo.role === 2013) {
        // 用户管理员权限
        const userManagerPermissions = [
          // 用户管理权限
          'user:add', 'user:edit', 'user:delete', 'user:view',
          
          // 会议预约权限
          'meeting:add', 'meeting:edit', 'meeting:view'
        ]
        
        userinfo.permissions = [...userManagerPermissions]
      }
      
      // 打印当前用户的权限，便于调试
      console.log('用户角色ID:', userinfo.role)
      console.log('用户权限列表:', userinfo.permissions)
    }
    
    // 重置用户信息
    const resetUserInfo = () => {
      userinfo.usertoken = ""
      userinfo.userid = ""
      userinfo.username = ""
      userinfo.realname = ""
      userinfo.captchaid = ""
      userinfo.role = 0
      userinfo.permissions = []
    }
    
    return { 
      someState,
      userinfo,
      initDefaultPermissions,
      resetUserInfo
    }
  },

  {
    persist: true,
  },
)